/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            allocationOrderEditInfo: {
                productId: '',
                specId: '',
                prodName: '',
                specName: '',
                shopName: '',
                srcShName: '',
                targetShopName: '',
                targetShName: '',
                productCount: '',
                shId: '',
                orderId: '',
                srcShId: '',
                targetShId: '',
                staffName: '',
                state: '',
                stateName: '',
                allocationOrders: [],
                shopHouses: [],
                shopId: ''
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('allocationOrderEdit', 'openEditAllocationOrderModal', function (_params) {
                $that.refreshEditAllocationOrderInfo();
                $('#editAllocationOrderModel').modal('show');
                vc.copyObject(_params, $that.allocationOrderEditInfo);
                $that._selectShopHouses(_params.srcShId);
            });
        },
        methods: {
            editAllocationOrderValidate: function () {
                return vc.validate.validate({
                    allocationOrderEditInfo: $that.allocationOrderEditInfo
                }, {
                    'allocationOrderEditInfo.orderId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "调拨单号不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "调拨单号格式不对"
                        },
                    ],
                    'allocationOrderEditInfo.prodName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商品名称不能为空"
                        }
                    ],
                    'allocationOrderEditInfo.specName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商品规格不能为空"
                        }
                    ],
                    'allocationOrderEditInfo.shopName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "源店铺不能为空"
                        }
                    ],
                    'allocationOrderEditInfo.srcShName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "源仓库不能为空"
                        }
                    ],
                    'allocationOrderEditInfo.targetShId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "目标仓库不能为空"
                        }
                    ],
                    'allocationOrderEditInfo.productCount': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商品数量不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "商品数量格式不对"
                        },
                    ],
                    'allocationOrderEditInfo.stateName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "状态不能为空"
                        }
                    ]
                });
            },
            _doEditAllocationOrder: function () {
                if (!$that.editAllocationOrderValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                if ($that.allocationOrderEditInfo.productCount <= 0) {
                    vc.toast("调拨数量不能小于等于0个");
                    return;
                }
                vc.http.apiPost(
                    '/allocationOrder/updateAllocationOrder',
                    JSON.stringify($that.allocationOrderEditInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editAllocationOrderModel').modal('hide');
                            vc.emit('allocationOrderManage', 'listAssetInventory', {});
                            vc.toast("修改成功");
                            return;
                        } else if(_json.code == 404){
                            vc.toast(_json.msg);
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            //查询仓库
            _selectShopHouses: function (shId) {
                var param = {
                    params: {
                        shopId: $that.allocationOrderEditInfo.shopId,
                        shId: shId,
                        communityId: vc.getCurrentCommunity().communityId,
                        flag: "1",
                        page: 1,
                        row: 50
                    }
                };
                vc.http.apiGet('/shopHouse.queryShopHouse',
                    param,
                    function (json, res) {
                        var listShopHouseData = JSON.parse(json);
                        $that.allocationOrderEditInfo.shopHouses = listShopHouseData.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    }
                );
            },
            refreshEditAllocationOrderInfo: function () {
                $that.allocationOrderEditInfo = {
                    productId: '',
                    specId: '',
                    prodName: '',
                    specName: '',
                    shopName: '',
                    srcShName: '',
                    targetShopName: '',
                    targetShName: '',
                    productCount: '',
                    shId: '',
                    orderId: '',
                    srcShId: '',
                    targetShId: '',
                    staffName: '',
                    state: '',
                    stateName: '',
                    allocationOrders: [],
                    shopHouses: [],
                    shopId: ''
                }
            }
        }
    });
})(window.vc);
